package com.librariy.util;

// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   MD5.java
import java.net.NetworkInterface;
import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StrUtil {
    public synchronized static String generateUUID() {
        return java.util.UUID.randomUUID().toString();
    }

    /**
     * 得到当前日期的格式化字符串
     * 
     * @param formatString
     *            如：yyyy(年)-MM(月)-dd(日)-HH(时)-mm(分)-ss(秒)-SSS(毫秒)
     * @return 格式化过的当前日期字符串
     */
    public static String getFormatDate(String formatString) {
        Date now = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat(formatString);
        String ret = sdf.format(now);
        return ret;
    }

    /**
     * 得到指定日期的格式化字符串
     * 
     * @param date
     *            指定的日期
     * @param formatString
     *            如：yyyy(年)-MM(月)-dd(日)-HH(时)-mm(分)-ss(秒)-SSS(毫秒)
     * @return 格式化过的日期字符串
     */
    public static String getFormatDate(Date date, String formatString) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatString);
        String ret = sdf.format(date);
        return ret;
    }

    /**
     * @return 当前日期
     */
    public static Date getCurrentDate() {
        Date now = new Date(System.currentTimeMillis());
        return now;
    }

    /**
     * 将格式化的日期字符串转换为日期。
     * 
     * @param dateString
     *            如：yyyy(年)-MM(月)-dd(日)-HH(时)-mm(分)-ss(秒)-SSS(毫秒)
     * @return 字符串转换后的日期。
     */
    public static Date formatDate(String dateString) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(dateString);
            return date;
        } catch (ParseException e) {
            return new Date();
        }
    }

    public static Date formatDate(String dateString, String formatString,Date defaultValue) {
        try {
            if(dateString==null){
                return defaultValue;
            }
            SimpleDateFormat sdf = new SimpleDateFormat(formatString);
            Date date = sdf.parse(dateString);
            return date;
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static Date formatSysDate() {
        String dateString = "";

        Calendar rightNow = Calendar.getInstance();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-mm-dd hh mm ss");
        dateString = fmt.format(rightNow.getTime());
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(dateString);
            return date;
        } catch (ParseException e) {
            return new Date();
        }
    }
    public static String[] split(String arraysString, String regularExpression) {
        return (arraysString == null || arraysString.equals("")) ? new String[0] : arraysString.split(regularExpression);
    }
    public static int parseInt(Object numberStr, int defaultValue) {
        return StrUtil.parseDouble(numberStr, defaultValue).intValue();
    }

    public static Double parseDouble(Object numberStr, double defaultValue) {
        if (numberStr == null)
            return defaultValue;
        Pattern pattern = Pattern.compile("^[\\-]{0,1}[0-9]+[.]{0,1}[0-9]{0,}$");
        Matcher matcher = pattern.matcher(numberStr.toString());
        if (matcher.matches()) {
            return Double.valueOf(numberStr.toString());
        } else {
            return defaultValue;
        }
    }

    public static String formatHidden(String src, int prefixLen, int suffixLen, char hiddenChar) {
        if (src == null)
            return "";
        if (src.length() <= (prefixLen + suffixLen))
            return src;
        char c[] = src.toCharArray();
        for (int i = prefixLen; i < (c.length - suffixLen); i++) {
            c[i] = hiddenChar;
        }
        return new String(c);
    }

    public static String getMd5String(String src,String defaultValue) {
        try {
            if (src == null) {
                return defaultValue;
            }
            MessageDigest mMessageDigest = MessageDigest.getInstance("MD5");
            byte b[] = mMessageDigest.digest(src.getBytes());
            return byte2Hex(b,defaultValue);
        } catch (Exception e) {
            return defaultValue;
        }
    }
    public static String byte2Hex(byte src[],String defaultValue) {
        try {
            if (src == null) {
                return defaultValue;
            }
            char HEX_C[] = "0123456789ABCDEF".toCharArray();
            StringBuffer sb = new StringBuffer("");
            for (int i = 0; i < src.length; i++) {
                sb.append(HEX_C[(byte) ((src[i] >> 4) & 0x0F)]);
                sb.append(HEX_C[(byte) ((src[i]) & 0x0F)]);
            }
            return sb.toString();
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static String generateFileName(String srcFileName,String assignedName) {
        try {
            if(assignedName==null||assignedName.equals("")){
                assignedName=StrUtil.generateUUID();
            }
            int index = srcFileName.lastIndexOf('.');
            return assignedName+((index < 0)?"":srcFileName.substring(index));
        } catch (Exception e) {
            return assignedName;
        }
    }
    /**获取第一块网卡物理地址*/
    public static String getMacAddress(String defaultValue){
        try {
            Enumeration<NetworkInterface> all=NetworkInterface.getNetworkInterfaces();
            if(!all.hasMoreElements()) throw new Exception("该设备未发现有效的网卡！");
            byte[] mac = all.nextElement().getHardwareAddress();
            char HEX_C[] = "0123456789ABCDEF".toCharArray();
            StringBuffer sb = new StringBuffer("");
            for (int i = 0; i < mac.length; i++) {
                sb.append(":");
                sb.append(HEX_C[(byte) ((mac[i] >> 4) & 0x0F)]);
                sb.append(HEX_C[(byte) ((mac[i]) & 0x0F)]);
            }
            sb.deleteCharAt(0);
            System.out.println("D/StrUtil:  本机MAC地址#["+sb+"]");
            return sb.toString();
        }catch (Exception e){
            System.out.println("D/StrUtil:  无法获取本机MAC地址！");
            e.printStackTrace();
            return defaultValue;
        }
    }
    /**URI地址连接*/
    public static String uriJoin(String base,String ...path){
        if(path==null||path.length==0){
            return base;
        }
        StringBuffer sb=new StringBuffer(base);
        for(String p:path){
            if(p==null||p.equals("")) continue;
            while(sb.charAt(sb.length()-1)=='/'){
                sb.deleteCharAt(sb.length()-1);
            }
            if(p.charAt(0)!='/'){
                sb.append("/");
            }
            sb.append(p);
        }
        return sb.toString();
    }
    public static void main(String[] args) {
        System.out.println(StrUtil.parseInt("12.578", -1));
        System.out.println(StrUtil.parseDouble("52.578.85", -1));
        System.out.println(StrUtil.parseInt("-12.578", -1));
        System.out.println(StrUtil.parseDouble("-52.578", -1));

        System.out.println(StrUtil.formatHidden("0123456789", 2, 3, '*'));
        System.out.println(StrUtil.formatHidden("0123456789", 5, 0, '*'));
        System.out.println(StrUtil.formatHidden("0123456789", 5, 5, '*'));
        System.out.println(StrUtil.formatHidden("张珊珊", 1, 0, '*'));
        
        System.out.println(StrUtil.generateFileName("\\g/dd.ee.txt",null));
        System.out.println(StrUtil.generateFileName("dd.ee.txt","测试"));
        System.out.println(StrUtil.generateFileName("dd.ee","测试adfadfasdf"));
        System.out.println(StrUtil.generateFileName("dd.ee.pdf","超大"));
        System.out.println(StrUtil.getMd5String("123",""));
        System.out.println(StrUtil.getMacAddress(""));
        
    }

}
